package com.css.dzj.dzjportal.modules.promulgate.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjportal.common.util.excel.ExcelData;
import com.css.dzj.dzjportal.common.util.excel.ExportExcelUtils;
import com.css.dzj.dzjportal.common.util.http.HttpClientUtil;
import com.css.dzj.dzjportal.modules.promulgate.entity.ApExecutorInfoEntity;
import com.css.dzj.dzjportal.modules.promulgate.service.ApExecutorInfoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ApExecutorInfoServiceImpl implements ApExecutorInfoService {
    @Autowired
    private HttpServletRequest request;
    @Value("${AP_URL}")
    private String apUrl;
    @Override
    public QueryResults<ApExecutorInfoEntity> selectAll(PageRequest pageRequest, Integer pageSize, Integer pageNo) {
        try {
            String msg = HttpClientUtil.doGet(apUrl + "/executorInfo/alllist");
            JSONArray jsonObject = JSON.parseArray(msg);
            List<ApExecutorInfoEntity> InfoEntityList = new ArrayList<>();
            if(jsonObject!=null && !"".equals(jsonObject)){
                for (int i = 0; i < jsonObject.size(); i++) {
                    Map<String,Object> eventMap = JSONObject.parseObject(jsonObject.get(i).toString());
                    ApExecutorInfoEntity executor = new ApExecutorInfoEntity();
                    executor.setId((String) eventMap.get("id"));
                    executor.setExecutorName((String) eventMap.get("executorName"));
                    executor.setExecutorOrganization((String) eventMap.get("executorOrganization"));
                    executor.setExecutorIdCard((String) eventMap.get("executorIdCard"));
                    executor.setExecutorSex((String) eventMap.get("executorSex"));
                    executor.setExecutorBirthday((String) eventMap.get("executorBirthday"));
                    executor.setExecutorPoliticStatus((String) eventMap.get("executorPoliticStatus"));
                    executor.setExecutorLevel((String) eventMap.get("executorLevel"));
                    executor.setExecutorNationality((String) eventMap.get("executorNationality"));
                    executor.setExecutorEducation((String) eventMap.get("executorEducation"));
                    executor.setExecutorCharacter((String) eventMap.get("executorCharacter"));
                    executor.setExecutorSuperviseStatus((String) eventMap.get("executorSuperviseStatus"));
                    executor.setExecutorLegalQualification((String) eventMap.get("executorLegalQualification"));
                    executor.setExecutorPhoneNum((String) eventMap.get("executorPhoneNum"));
                    executor.setExecutorCode((String) eventMap.get("executorCode"));
                    executor.setExecutorMajor((String) eventMap.get("executorMajor"));
                    executor.setExecutorCertifyingAuthority((String) eventMap.get("executorCertifyingAuthority"));
                    executor.setExecutorArea((String) eventMap.get("executorArea"));
                    executor.setExecutorCategory((String) eventMap.get("executorCategory"));
                    executor.setExecutorCardInDate((String) eventMap.get("executorCardInDate"));
                    executor.setExecutorCreateTime((String) eventMap.get("executorCreateTime"));
                    executor.setExecutorUpdateTime((String) eventMap.get("executorUpdateTime"));
                    executor.setExecutorDelFlag((String) eventMap.get("executorDelFlag"));
                    executor.setExecutorLocation((String) eventMap.get("executorLocation]"));
                    System.out.println();
                    InfoEntityList.add(executor);
                }
            }
            List<ApExecutorInfoEntity> currentPageList = new ArrayList<>();
            if (InfoEntityList != null && InfoEntityList.size() > 0) {
                int currIdx = (pageNo > 1 ? (pageNo - 1) * pageSize : 0);
                for (int i = 0; i < pageSize && i < InfoEntityList.size() - currIdx; i++) {
                    ApExecutorInfoEntity data = InfoEntityList.get(currIdx + i);
                    System.out.println(data);
                    currentPageList.add(data);
                }
            }
            QueryResults<ApExecutorInfoEntity> rwsQueryResults = new QueryResults(currentPageList, (long)pageRequest.getPageSize(), pageRequest.getOffset(), InfoEntityList.size());
            return rwsQueryResults;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public QueryResults<ApExecutorInfoEntity> getSelectInfo(PageRequest pageRequest, Integer pageSize, Integer pageNo,String executorName, String executorCharacter, String executorArea, String executorCategory) {
        try {
            String msg = HttpClientUtil.doGet(apUrl + "/executorInfo/getSelectCon?executorName="+executorName
                    +"&executorCharacter="+executorCharacter+"&executorArea="+executorArea+"&executorCategory="+executorCategory);
            JSONArray jsonObject = JSON.parseArray(msg);
            List<ApExecutorInfoEntity> InfoEntityList = new ArrayList<>();
            if(jsonObject!=null && !"".equals(jsonObject)){
                for (int i = 0; i < jsonObject.size(); i++) {
                    Map<String,Object> eventMap = JSONObject.parseObject(jsonObject.get(i).toString());
                    System.out.println(eventMap);
                    ApExecutorInfoEntity executor = new ApExecutorInfoEntity();
                    executor.setId((String) eventMap.get("id"));
                    executor.setExecutorName((String) eventMap.get("executorName"));
                    executor.setExecutorOrganization((String) eventMap.get("executorOrganization"));
                    executor.setExecutorIdCard((String) eventMap.get("executorIdCard"));
                    executor.setExecutorSex((String) eventMap.get("executorSex"));
                    executor.setExecutorBirthday((String) eventMap.get("executorBirthday"));
                    executor.setExecutorPoliticStatus((String) eventMap.get("executorPoliticStatus"));
                    executor.setExecutorLevel((String) eventMap.get("executorLevel"));
                    executor.setExecutorNationality((String) eventMap.get("executorNationality"));
                    executor.setExecutorEducation((String) eventMap.get("executorEducation"));
                    executor.setExecutorCharacter((String) eventMap.get("executorCharacter"));
                    executor.setExecutorSuperviseStatus((String) eventMap.get("executorSuperviseStatus"));
                    executor.setExecutorLegalQualification((String) eventMap.get("executorLegalQualification"));
                    executor.setExecutorPhoneNum((String) eventMap.get("executorPhoneNum"));
                    executor.setExecutorCode((String) eventMap.get("executorCode"));
                    executor.setExecutorMajor((String) eventMap.get("executorMajor"));
                    executor.setExecutorCertifyingAuthority((String) eventMap.get("executorCertifyingAuthority"));
                    executor.setExecutorArea((String) eventMap.get("executorArea"));
                    executor.setExecutorCategory((String) eventMap.get("executorCategory"));
                    executor.setExecutorCardInDate((String) eventMap.get("executorCardInDate"));
                    executor.setExecutorCreateTime((String) eventMap.get("executorCreateTime"));
                    executor.setExecutorUpdateTime((String) eventMap.get("executorUpdateTime"));
                    executor.setExecutorDelFlag((String) eventMap.get("executorDelFlag"));
                    executor.setExecutorLocation((String) eventMap.get("executorLocation]"));
                    InfoEntityList.add(executor);
                }
            }
            List<ApExecutorInfoEntity> currentPageList = new ArrayList<>();
            if (InfoEntityList != null && InfoEntityList.size() > 0) {
                int currIdx = (pageNo > 1 ? (pageNo - 1) * pageSize : 0);
                for (int i = 0; i < pageSize && i < InfoEntityList.size() - currIdx; i++) {
                    ApExecutorInfoEntity data = InfoEntityList.get(currIdx + i);
                    currentPageList.add(data);
                }
            }
            QueryResults<ApExecutorInfoEntity> rwsQueryResults = new QueryResults(currentPageList, (long)pageRequest.getPageSize(), pageRequest.getOffset(), InfoEntityList.size());
            return rwsQueryResults;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public  List<ApExecutorInfoEntity> getIds(String ids) {
        try {
            String msg = HttpClientUtil.doGet(apUrl + "/executorInfo/getIds?ids="+ids);
            JSONArray jsonObject = JSON.parseArray(msg);
            List<ApExecutorInfoEntity> InfoEntityList = new ArrayList<>();
            if(jsonObject!=null && !"".equals(jsonObject)){
                for (int i = 0; i < jsonObject.size(); i++) {
                    Map<String,Object> eventMap = JSONObject.parseObject(jsonObject.get(i).toString());  // System.out.println(eventMap);
                    ApExecutorInfoEntity executor = new ApExecutorInfoEntity();
                    executor.setId((String) eventMap.get("id"));
                    executor.setExecutorName((String) eventMap.get("executorName"));
                    executor.setExecutorOrganization((String) eventMap.get("executorOrganization"));
                    executor.setExecutorIdCard((String) eventMap.get("executorIdCard"));
                    executor.setExecutorSex((String) eventMap.get("executorSex"));
                    executor.setExecutorBirthday((String) eventMap.get("executorBirthday"));
                    executor.setExecutorPoliticStatus((String) eventMap.get("executorPoliticStatus"));
                    executor.setExecutorLevel((String) eventMap.get("executorLevel"));
                    executor.setExecutorNationality((String) eventMap.get("executorNationality"));
                    executor.setExecutorEducation((String) eventMap.get("executorEducation"));
                    executor.setExecutorCharacter((String) eventMap.get("executorCharacter"));
                    executor.setExecutorSuperviseStatus((String) eventMap.get("executorSuperviseStatus"));
                    executor.setExecutorLegalQualification((String) eventMap.get("executorLegalQualification"));
                    executor.setExecutorPhoneNum((String) eventMap.get("executorPhoneNum"));
                    executor.setExecutorCode((String) eventMap.get("executorCode"));
                    executor.setExecutorMajor((String) eventMap.get("executorMajor"));
                    executor.setExecutorCertifyingAuthority((String) eventMap.get("executorCertifyingAuthority"));
                    executor.setExecutorArea((String) eventMap.get("executorArea"));
                    executor.setExecutorCategory((String) eventMap.get("executorCategory"));
                    executor.setExecutorCardInDate((String) eventMap.get("executorCardInDate"));
                    executor.setExecutorCreateTime((String) eventMap.get("executorCreateTime"));
                    executor.setExecutorUpdateTime((String) eventMap.get("executorUpdateTime"));
                    executor.setExecutorDelFlag((String) eventMap.get("executorDelFlag"));
                    executor.setExecutorLocation((String) eventMap.get("executorLocation]"));
                    InfoEntityList.add(executor);
                }
            }
          return InfoEntityList;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public List<ApExecutorInfoEntity> getalls(String executorName, String executorCharacter, String executorArea, String executorCategory) {
        try {
            String msg = HttpClientUtil.doGet(apUrl + "/executorInfo/getSelectCon?executorName="+executorName
                    +"&executorCharacter="+executorCharacter+"&executorArea="+executorArea+"&executorCategory="+executorCategory);
            JSONArray jsonObject = JSON.parseArray(msg);
            List<ApExecutorInfoEntity> InfoEntityList = new ArrayList<>();
            if(jsonObject!=null && !"".equals(jsonObject)){
                for (int i = 0; i < jsonObject.size(); i++) {
                    Map<String,Object> eventMap = JSONObject.parseObject(jsonObject.get(i).toString());
                    System.out.println();
                    ApExecutorInfoEntity executor = new ApExecutorInfoEntity();
                    executor.setId((String) eventMap.get("id"));
                    executor.setExecutorName((String) eventMap.get("executorName"));
                    executor.setExecutorOrganization((String) eventMap.get("executorOrganization"));
                    executor.setExecutorIdCard((String) eventMap.get("executorIdCard"));
                    executor.setExecutorSex((String) eventMap.get("executorSex"));
                    executor.setExecutorBirthday((String) eventMap.get("executorBirthday"));
                    executor.setExecutorPoliticStatus((String) eventMap.get("executorPoliticStatus"));
                    executor.setExecutorLevel((String) eventMap.get("executorLevel"));
                    executor.setExecutorNationality((String) eventMap.get("executorNationality"));
                    executor.setExecutorEducation((String) eventMap.get("executorEducation"));
                    executor.setExecutorCharacter((String) eventMap.get("executorCharacter"));
                    executor.setExecutorSuperviseStatus((String) eventMap.get("executorSuperviseStatus"));
                    executor.setExecutorLegalQualification((String) eventMap.get("executorLegalQualification"));
                    executor.setExecutorPhoneNum((String) eventMap.get("executorPhoneNum"));
                    executor.setExecutorCode((String) eventMap.get("executorCode"));
                    executor.setExecutorMajor((String) eventMap.get("executorMajor"));
                    executor.setExecutorCertifyingAuthority((String) eventMap.get("executorCertifyingAuthority"));
                    executor.setExecutorArea((String) eventMap.get("executorArea"));
                    executor.setExecutorCategory((String) eventMap.get("executorCategory"));
                    executor.setExecutorCardInDate((String) eventMap.get("executorCardInDate"));
                    executor.setExecutorCreateTime((String) eventMap.get("executorCreateTime"));
                    executor.setExecutorUpdateTime((String) eventMap.get("executorUpdateTime"));
                    executor.setExecutorDelFlag((String) eventMap.get("executorDelFlag"));
                    executor.setExecutorLocation((String) eventMap.get("executorLocation]"));
                    InfoEntityList.add(executor);
                }
            }
           return InfoEntityList;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public ApExecutorInfoEntity getExecutorInfo(String id) {
        try {
            if(StringUtils.isEmpty(id)){
                throw new RuntimeException("id不能为空");
            }else{
                String msg = HttpClientUtil.doGet(apUrl + "/executorInfo/getOne?id="+id);
                Object JSONObject = JSON.parseObject(msg);
                ApExecutorInfoEntity executor = new ApExecutorInfoEntity();
                executor.setId((String) ((JSONObject) JSONObject).get("id"));
                executor.setExecutorName((String) ((JSONObject) JSONObject).get("executorName"));
                executor.setExecutorOrganization((String) ((JSONObject) JSONObject).get("executorOrganization"));
                executor.setExecutorIdCard((String) ((JSONObject) JSONObject).get("executorIdCard"));
                executor.setExecutorSex((String) ((JSONObject) JSONObject).get("executorSex"));
                executor.setExecutorBirthday((String) ((JSONObject) JSONObject).get("executorBirthday"));
                executor.setExecutorPoliticStatus((String) ((JSONObject) JSONObject).get("executorPoliticStatus"));
                executor.setExecutorLevel((String) ((JSONObject) JSONObject).get("executorLevel"));
                executor.setExecutorNationality((String) ((JSONObject) JSONObject).get("executorNationality"));
                executor.setExecutorEducation((String) ((JSONObject) JSONObject).get("executorEducation"));
                executor.setExecutorCharacter((String) ((JSONObject) JSONObject).get("executorCharacter"));
                executor.setExecutorSuperviseStatus((String) ((JSONObject) JSONObject).get("executorSuperviseStatus"));
                executor.setExecutorLegalQualification((String) ((JSONObject) JSONObject).get("executorLegalQualification"));
                executor.setExecutorPhoneNum((String) ((JSONObject) JSONObject).get("executorPhoneNum"));
                executor.setExecutorCode((String) ((JSONObject) JSONObject).get("executorCode"));
                executor.setExecutorMajor((String) ((JSONObject) JSONObject).get("executorMajor"));
                executor.setExecutorCertifyingAuthority((String) ((JSONObject) JSONObject).get("executorCertifyingAuthority"));
                executor.setExecutorArea((String) ((JSONObject) JSONObject).get("executorArea"));
                executor.setExecutorCategory((String) ((JSONObject) JSONObject).get("executorCategory"));
                executor.setExecutorCardInDate((String) ((JSONObject) JSONObject).get("executorCardInDate"));
                executor.setExecutorCreateTime((String) ((JSONObject) JSONObject).get("executorCreateTime"));
                executor.setExecutorUpdateTime((String) ((JSONObject) JSONObject).get("executorUpdateTime"));
                executor.setExecutorDelFlag((String) ((JSONObject) JSONObject).get("executorDelFlag"));
                executor.setExecutorLocation((String) ((JSONObject) JSONObject).get("executorLocation]"));
                return executor;
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public void exportExecutorToExcel(HttpServletResponse response, List<ApExecutorInfoEntity> executorEntities) {
        if (executorEntities == null || executorEntities.size() < 0) {
            throw new RuntimeException("执法人员为空");
        }
        // 创建导出实体
        ExcelData data = new ExcelData();
        data.setName("执法人员表");
        /**
         * 设置Excel标题
         */
        ArrayList<String> titles = new ArrayList<>();
        titles.add("姓名");
        titles.add("执法人员性质");
        titles.add("联系电话");
        titles.add("执法区域");
        titles.add("执法类别");
        titles.add("证件有效期");
        data.setTitles(titles);
        /**
         * 处理行数据
         */
        ArrayList<List<Object>> rows = new ArrayList<>();
        for (ApExecutorInfoEntity apExecutorInfoEntity : executorEntities) {
            ArrayList<Object> row = new ArrayList<>();
            row.add(apExecutorInfoEntity.getExecutorName());
            if("1".equals(apExecutorInfoEntity.getExecutorCharacter())){
                row.add("公务员");
            }
            if("2".equals(apExecutorInfoEntity.getExecutorCharacter())){
                row.add("聘任制公务员");
            }
            if("3".equals(apExecutorInfoEntity.getExecutorCharacter())){
                row.add("事业编制工作人员");
            }
            if("4".equals(apExecutorInfoEntity.getExecutorCharacter())){
                row.add("企业人员");
            }
            if("5".equals(apExecutorInfoEntity.getExecutorCharacter())){
                row.add("执法辅助人员");
            }
            row.add(apExecutorInfoEntity.getExecutorPhoneNum());
            row.add(apExecutorInfoEntity.getExecutorArea());
            row.add(apExecutorInfoEntity.getExecutorCategory());
            row.add(apExecutorInfoEntity.getExecutorCardInDate());
            rows.add(row);
        }
        data.setRows(rows);
        try {
            // 导出
            ExportExcelUtils.exportExcel(request,response, "执法人员表.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
